/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "car_movectrl.h"
#include "uart_dev.h"
#include "can_dev.h"
#include "HX711.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId Wheel_ControlHandle;
osThreadId Wheel_OdomHandle;
osThreadId Wheel_VoltageHandle;
osThreadId Agv_TxmessageHandle;
osThreadId Get_WeightHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void wheel_control(void const * argument);
void wheel_odom(void const * argument);
void wheel_voltage(void const * argument);
void agv_txmessage(void const * argument);
void get_weight(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of Wheel_Control */
  osThreadDef(Wheel_Control, wheel_control, osPriorityHigh, 0, 128);
  Wheel_ControlHandle = osThreadCreate(osThread(Wheel_Control), NULL);

  /* definition and creation of Wheel_Odom */
  osThreadDef(Wheel_Odom, wheel_odom, osPriorityHigh, 0, 128);
  Wheel_OdomHandle = osThreadCreate(osThread(Wheel_Odom), NULL);

  /* definition and creation of Wheel_Voltage */
  osThreadDef(Wheel_Voltage, wheel_voltage, osPriorityHigh, 0, 128);
  Wheel_VoltageHandle = osThreadCreate(osThread(Wheel_Voltage), NULL);

  /* definition and creation of Agv_Txmessage */
  osThreadDef(Agv_Txmessage, agv_txmessage, osPriorityHigh, 0, 128);
  Agv_TxmessageHandle = osThreadCreate(osThread(Agv_Txmessage), NULL);

  /* definition and creation of Get_Weight */
  osThreadDef(Get_Weight, get_weight, osPriorityHigh, 0, 128);
  Get_WeightHandle = osThreadCreate(osThread(Get_Weight), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_wheel_control */
/**
  * @brief  Function implementing the Wheel_Control thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_wheel_control */
void wheel_control(void const * argument)
{
  /* USER CODE BEGIN wheel_control */
  /* Infinite loop */
  while(1){
		Car_Movectrl();
		HAL_Delay(5);
	}
  /* USER CODE END wheel_control */
}

/* USER CODE BEGIN Header_wheel_odom */
/**
* @brief Function implementing the Wheel_Odom thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_wheel_odom */
void wheel_odom(void const * argument)
{
  /* USER CODE BEGIN wheel_odom */
  /* Infinite loop */
  while(1){
		Can_SendMessage(&hcan1,CANCMD_GetMotorData,0,0);
		HAL_Delay(5);
	}
  /* USER CODE END wheel_odom */
}

/* USER CODE BEGIN Header_wheel_voltage */
/**
* @brief Function implementing the Wheel_Voltage thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_wheel_voltage */
void wheel_voltage(void const * argument)
{
  /* USER CODE BEGIN wheel_voltage */
  /* Infinite loop */
  while(1){
		Can_SendMessage(&hcan1,CANCMD_GetVoltage,0,0);
		HAL_Delay(5);
	}
  /* USER CODE END wheel_voltage */
}

/* USER CODE BEGIN Header_agv_txmessage */
/**
* @brief Function implementing the Agv_Txmessage thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_agv_txmessage */
void agv_txmessage(void const * argument)
{
  /* USER CODE BEGIN agv_txmessage */
	const portTickType xDelay = pdMS_TO_TICKS(100);
	//static portTickType xLastWakeTime;
  /* Infinite loop */
  while(1){
		Uart_Transmit(&huart5,Txdata,TX_BUFF_LEN);
		vTaskDelay(xDelay);
		//HAL_Delay(5);
	}
  /* USER CODE END agv_txmessage */
}

/* USER CODE BEGIN Header_get_weight */
/**
* @brief Function implementing the Get_Weight thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_get_weight */
void get_weight(void const * argument)
{
  /* USER CODE BEGIN get_weight */
  /* Infinite loop */
  while(1)
  {
    my_car.weight = Get_Weight();
		HAL_Delay(5);
  }
  /* USER CODE END get_weight */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */
